﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    treeGrid: 'treeGrid'
}).use(['layer', 'table', 'laypage', 'laytpl', 'laydate', 'tree', 'treeGrid', 'jquery'], function () {
    var $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate,
        player = parent.layer || layui.layer,
        tree = layui.tree,
        treeGrid = layui.treeGrid; //很重要
    var authorityAdminAuthorityListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                field: 'iSort',
                order: 'asc',
                rowData: undefined,//选中行信息
                listsUrl: "/Authority/AdminAuthority/LoadListInfo",
                addUrl: "/Authority/AdminAuthority/Add",
                modifyUrl: "/Authority/AdminAuthority/Modify",
                removeUrl: "/Authority/AdminAuthority/Remove",
                authorizeUrl: "/Authority/AdminAuthority/Authorize"
            };

            //this.Parameters.ac
            //    .pushQueue(function () {
            //        CommonLogic.loadButtonsInfo(function () {
            //            self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //        });
            //    })
            //    .pushQueue(function () {
            //        self.initPage();
            //        self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //    })
            //    .pushQueue(this.bindEvent)
            //    .exec();

            this.initPage();
            this.bindEvent();
        },
        //初始化列表
        initPage: function () {
            var treeTable = treeGrid.render({
                elem: '#roleList',
                url: self.Parameters.listsUrl,
                cellMinWidth: 100,
                height: "full-90",
                idField: 'gCode',//必須字段
                treeId: 'gCode',//树形id字段名称
                treeUpId: 'gParentCode',//树形父id字段名称
                treeShowName: 'sName',//以树形式显示的字段
                isOpenDefault: true,//节点默认是展开还是折叠【默认展开】
                cheDisabled: true,
                page: false,
                cols: [[
                    { type: 'radio' },//checkbox 
                    { field: 'sName', title: '角色名称', width: '180'},
                    { field: 'sContent', title: '备注', width: '300' },
                    { title: '操作', toolbar: '#gridTool', width: '150', align: 'left' }
                ]],
                onRadio: function (obj) {
                    self.Parameters.rowData = obj;
                }
            });
        },
        //绑定事件
        bindEvent: function () {

            //监听(列表操作)
            treeGrid.on('tool(roleList)', function (obj) {
                var data = obj.data;
                self.Parameters.rowData = data;
                //新增子节点
                if (obj.event === 'funBtnAdd') {

                    self.logicFunc.addInfo();
                }
                //修改
                else if (obj.event === 'funBtnEdit') {
                    self.logicFunc.modifyInfo();
                }
                //授权
                else if (obj.event === 'funBtnFilter') {
                    self.logicFunc.authorizeInfo();
                }
                //删除
                else if (obj.event === 'funBtnRemove') {
                    self.logicFunc.removeInfo();
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //新增根节点
            $("#funBtnAddRoot").on("click", function () {
                self.logicFunc.addInfo();
            });

            //上移
            $("#funBtnTop").on("click", function () {
                self.logicFunc.move(1);
            });

            //下移
            $("#funBtnBottom").on("click", function () {
                self.logicFunc.move(2);
            });

            //对于单选框按钮点击行的选中功能：
            $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
                /*当单击表格行时,把单选按钮设为选中状态*/
                var tableDiv;
                var index = $(this).attr('data-index');
                var tableBox = $(this).parents('.layui-table-box');
                //存在固定列
                if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                    tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
                } else {
                    tableDiv = tableBox.find(".layui-table-body.layui-table-main");
                }
                var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.layui-table-cell div.layui-form-radio I");
                if (checkCell.length > 0) {
                    checkCell.click();
                }
            });
            //对td的单击事件进行拦截停止，防止事件冒泡再次触发上述的单击事件  将此代码在页面初始化后执行一次即可以。
            $(document).on("click", "td div.layui-table-cell div.layui-form-radio", function (e) {
                e.stopPropagation();
            });

        },
        //逻辑方法
        logicFunc: {

            ///获取数据
            loadListInfo: function () {
                treeGrid.reload("roleList", {
                    page: {
                        curr: 1
                    },
                    where: {
                        parameters: GrdLayUI.zip($("#txtKeyWord").val())
                    }
                });
            },

            //增加信息
            addInfo: function () {
                var title = "增加根节点信息";
                var url = self.Parameters.addUrl;
                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {
                    var data = {
                        gParentCode: self.Parameters.rowData.gCode,
                        iSort: self.Parameters.rowData.iSort + 1
                    };
                    title = "增加子节点信息";
                    url = self.Parameters.addUrl + '?data=' + GrdLayUI.zip(data);
                }
                var index = layui.layer.open({
                    title: title,
                    type: 2,
                    resize: false,
                    area: ['500px', '300px'],
                    content: url,
                    end: function () {
                        $(window).unbind("resize");
                    }
                });
            },

            //修改信息
            modifyInfo: function () {

                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {
                    var index = layui.layer.open({
                        title: "修改菜单信息",
                        type: 2,
                        resize: false,
                        area: ['500px', '300px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(self.Parameters.rowData),
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                }
            },

            //删除信息
            removeInfo: function () {
                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {

                    GrdLayUI.confirm("确定删除信息吗？", function () {
                        var arrayObj = new Array();//创建一个数组
                        arrayObj.push(self.Parameters.rowData.gCode);
                        var json = {
                            parameters: arrayObj
                        };
                        GrdLayUI.ajaxPost({
                            url: self.Parameters.removeUrl,
                            data: json,
                            success: function (result) {
                                if (result.Code == 1000) {
                                    GrdLayUI.msg(result.Msg, "成功", function () {
                                        self.logicFunc.loadListInfo();
                                    });
                                } else {
                                    GrdLayUI.msg(result.Msg, "警告");
                                }
                            }
                        });
                    });
                }
            },

            //授权
            authorizeInfo: function () {
                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {

                    var index = layui.layer.open({
                        title: "角色授权信息",
                        type: 2,
                        resize: false,
                        area: ['600px', '700px'],
                        content: self.Parameters.authorizeUrl + '?data=' + self.Parameters.rowData.gCode,
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                }
            }
        },
        //工具
        tools: {

        }
    };
    authorityAdminAuthorityListLogic.init();
});
